package cn.tedu.dimanage.apply.webapi.service;

import cn.tedu.dimanage.apply.service.IApplyService;
import cn.tedu.dimanage.apply.webapi.mapper.ApplyMapper;
import cn.tedu.dimanage.common.ex.ServiceException;
import cn.tedu.dimanage.common.web.State;
import cn.tedu.dimanage.item.service.IItemService;
import cn.tedu.dimanage.pojo.dto.ApplyAddNewDTO;
import cn.tedu.dimanage.pojo.dto.ApplyCheckDTO;
import cn.tedu.dimanage.pojo.dto.ApplyUpdateDTO;
import cn.tedu.dimanage.pojo.entity.Apply;
import cn.tedu.dimanage.pojo.vo.ApplyDetailsVO;
import cn.tedu.dimanage.pojo.vo.ItemDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Classname: ApplyServiceImpl
 * @Author: bromide
 * @CreateTime: 2022/6/21--下午11:04
 * @Version: V1.0
 * @Description:
 */
@Service
@Slf4j
public class ApplyServiceImpl implements IApplyService {
    @Autowired
    private ApplyMapper applyMapper;

    @DubboReference
    private IItemService dubboItemService;


    @Override
    public void addApply(ApplyAddNewDTO applyAddNewDTO) {
        Apply apply = new Apply();
        BeanUtils.copyProperties(applyAddNewDTO,apply);
        // 补全数据
        // 通过itemService查找到用户需要申请到物资名称
        ItemDetailVO itemDetails = dubboItemService.getItemDetailsById(applyAddNewDTO.getItemId());
        String itemName = itemDetails.getName();
        apply.setItemName(itemName);
        apply.setGmtCreate(LocalDateTime.now());
        int row = applyMapper.addApply(apply);
        if (row != 1){
            throw new ServiceException(State.ERR_INSERT,"服务器忙,提交申请失败,请稍后再试");
        }
    }

    @Override
    public List<ApplyDetailsVO> selectApplyList(String keyword) {
        return applyMapper.selectApplyList(keyword);
    }

    @Override
    public List<ApplyDetailsVO> selectApplyListByUserId(Long userId) {
        return applyMapper.selectApplyListByUserId(userId);
    }

    @Override
    public void updateApplyByAdmin(ApplyCheckDTO applyCheckDTO) {
        ApplyDetailsVO applyDetailsVO = applyMapper.selectApplyById(applyCheckDTO.getId());
        Integer checkState = applyDetailsVO.getCheckState();
        if (checkState == null){
            // 状态没有被审核
            applyCheckDTO.setCheckState(1);
            applyCheckDTO.setGmtModified(LocalDateTime.now());
            // 当申请成功之后,修改item表中的数据
            // 如果表中有同名数据,则直接修改item表中的个数
            ItemDetailVO itemDetailVO = null;
            String itemName = applyDetailsVO.getItemName();
            try {
                itemDetailVO = dubboItemService.getItemDetailsByName(itemName);
            }catch (Exception e){
                log.debug("物资表中没有名为:{}的物资数据",itemName);
                throw new ServiceException(State.ERR_ITEM_NOT_FOUND,"申请物资失败,物资不存在");
            }
            log.debug("物资表中存在名为:{}的物资数据",itemName);
            Integer count = applyDetailsVO.getCount();
            if (count > itemDetailVO.getInventory()){
                throw new ServiceException(State.ERR_APPLY_TOO_MUCH,"该申请数量超过库存,审核失败");
            }else {
                int row = applyMapper.updateApplyByAdmin(applyCheckDTO);
                if (row != 1){
                    throw new ServiceException(State.ERR_UPDATE,"服务器忙,请稍后再试");
                }else {
                    if (applyCheckDTO.getCheckState() == 1){
                        itemDetailVO.setInventory(itemDetailVO.getInventory()-count);
                    }
                    dubboItemService.updateItem(itemDetailVO);
                }
            }
            return;
        }
        // 如果提交的要修改的信息已经被同意,那么将不同意修改
        throw new ServiceException(State.ERR_APPLY_BAD_STATE,"申请已被审核,不允许修改");
    }

    @Override
    public void updateApplyByUser(ApplyUpdateDTO applyUpdateDTO) {
        String itemName = dubboItemService.getItemDetailsById(applyUpdateDTO.getId()).getName();
        applyUpdateDTO.setItemName(itemName);
        ApplyDetailsVO applyDetailsVO = applyMapper.selectApplyById(applyUpdateDTO.getId());
        System.out.println("================" + applyDetailsVO);
        Integer checkState = applyDetailsVO.getCheckState();
        if (checkState == null){
            applyUpdateDTO.setGmtCreate(LocalDateTime.now());
            int row = applyMapper.updateApplyByUser(applyUpdateDTO);
            if (row != 1){
                throw new ServiceException(State.ERR_UPDATE,"服务器忙,请稍后再试");
            }
            return;
        }
        // 如果提交的要修改的信息已经被同意或拒绝,那么将不同意修改
        throw new ServiceException(State.ERR_APPLY_BAD_STATE,"捐献申请已被同意,不允许修改");
    }

    @Override
    public void deleteApplyById(Long id) {
        ApplyDetailsVO applyDetailsVO = applyMapper.selectApplyById(id);
        Integer checkState = applyDetailsVO.getCheckState();
        if (checkState == null || checkState != 1){
            int row = applyMapper.deleteApplyById(id);
            if (row != 1){
                throw new ServiceException(State.ERR_DELETE,"服务器忙,请稍后再试");
            }
        }else {
            // 如果提交的要修改的信息已经被同意,那么将不同意删除
            throw new ServiceException(State.ERR_APPLY_BAD_STATE,"捐献申请已被同意,不允许删除");
        }

    }

    @Override
    public List<ApplyDetailsVO> selectApplyOut(String keyword) {
        return applyMapper.selectApplyOut(keyword);
    }
}
